package com.lcy.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcy.reggie.common.Result;
import com.lcy.reggie.entity.ShoppingCart;
import com.lcy.reggie.service.ShoppingCartService;
import com.lcy.reggie.mapper.ShoppingCartMapper;
import com.lcy.reggie.utils.BaseContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * @author ChunBaBa_
 * @description 针对表【shopping_cart(购物车)】的数据库操作Service实现
 * @createDate 2023-01-17 16:11:41
 */
@Transactional
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart>
        implements ShoppingCartService {

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public Result<ShoppingCart> add(ShoppingCart shoppingCart) {
        //1.设置user_id，保存是哪个用户下的单
        Long user_id = BaseContext.getCurrentId();

        //2.1 判断添加的是套餐还是菜品
        //2.2 如果添加的是菜品
        ShoppingCart one = null;
        if (shoppingCart.getDishId() != null) {
            //2.3判断是否有完全相同的菜品，如果有，则number+1
            one = this.query().eq("dish_id", shoppingCart.getDishId()).one();
        }else{
            //2.4如果添加的是套餐，判断有没有相同的套餐
            one = this.query().eq("setmeal_id", shoppingCart.getSetmealId()).one();
        }
        if (one != null) {
            one.setNumber(one.getNumber() + 1);
            this.updateById(one);
            return Result.success(one);
        }
        //3.如果没有相同的，直接新增
        shoppingCart.setUserId(user_id);
        shoppingCart.setNumber(1);
        //3.1 这里不能自动填充 所以需要手动补充create_time
        shoppingCart.setCreateTime(LocalDateTime.now());
        this.save(shoppingCart);
        return Result.success(shoppingCart);
    }

    /**
     * 选中商品-1
     * @param shoppingCart
     * @return
     */
    @Override
    public Result<ShoppingCart> sub(ShoppingCart shoppingCart) {
        ShoppingCart one = null;
        //1.先判断减少的是菜品还是套餐
        if(shoppingCart.getDishId()!=null){
           one = this.query().eq("dish_id", shoppingCart.getDishId()).one();
        }
        else {
            one = this.query().eq("setmeal_id", shoppingCart.getSetmealId()).one();
        }
        //2.判断购物车本商品选中数量
        if(one.getNumber()>1){
            //2.1 大于1，直接-1即可
            one.setNumber(one.getNumber()-1);
            updateById(one);
            return Result.success(one);
        }else {
            //2.2 等于1 ,直接删除该数据
            removeById(one);

            //3.注意：这里前端接收的是data.number数据来判断显示
            //所以如果1-1的话，直接给前端返回的number=0的shoppingCart对象就行了
            shoppingCart.setNumber(0);
            return Result.success(shoppingCart);
        }
    }

    /**
     * 显示购物车数据
     * @return
     */
    @Override
    public Result<List<ShoppingCart>> listResult() {
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> list = this.query()
                .eq("user_id", userId)
                .orderByDesc("create_time")
                .list();
        return Result.success(list);
    }

    /**
     * 清空购物车
     * @return
     */
    @Override
    public Result<String> clean() {
        //1.获取用户id
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> list = query().eq("user_id", userId).list();
        removeBatchByIds(list);
        return Result.success("清空成功！");
    }
}




